package com.kcs.fxmargin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.action.form.SetupParameterActionForm;
import com.kcs.fxmargin.model.MSetupParam;
import com.kcs.fxmargin.service.CommonService;
import com.kcs.fxmargin.service.SetupParameterService;

public class SetupParameterAction extends FXAction {
	private Logger log = Logger.getLogger(Constant.LOG_FX);

    private CommonService common 			= new CommonService();
	private SetupParameterService service 	= new SetupParameterService();

	
	@Override
	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{ 
		SetupParameterActionForm actionForm = null;
		
		try{
			log.debug("SetupParameterAction : Start");
			
			actionForm = (SetupParameterActionForm) form;
			
			if(actionForm.getAction() == null || "init".equals(actionForm.getAction())){
				MSetupParam param = service.getSetupParamter();
				
				if(param != null){
					log.debug("Id = " + param.getId());
					log.debug("Initial Margin 		= " + param.getInitialMargin());
					log.debug("Maintenance Margin 	= " + param.getMaintenanceMargin());
					log.debug("Fx Margin Limit 	= " + param.getFxMarginLimit());
					log.debug("RowVersion 			= " + param.getRowVersion());	
					
					actionForm.setId(param.getId());
					actionForm.setInitialMargin(param.getInitialMargin());
					actionForm.setMaintenanceMargin(param.getMaintenanceMargin());
					actionForm.setFxMarginLimit(param.getFxMarginLimit());
					actionForm.setRowVersion(param.getRowVersion());
					actionForm.setMessage("");
					
				}
				
			} else if("update".equals(actionForm.getAction())){
				actionForm.setMessage("m_save_fail");
				MSetupParam param = new MSetupParam();
				
				log.debug("Id 					= " + actionForm.getId());
				log.debug("Initial Margin 		= " + actionForm.getInitialMargin());
				log.debug("Maintenance Margin 	= " + actionForm.getMaintenanceMargin());
				log.debug("Fx Margin Limit 	= " + actionForm.getFxMarginLimit());
				log.debug("RowVersion 			= " + actionForm.getRowVersion());	
				
				param.setId(actionForm .getId());
				param.setInitialMargin(actionForm.getInitialMargin());
				param.setMaintenanceMargin(actionForm.getMaintenanceMargin());
				param.setFxMarginLimit(actionForm.getFxMarginLimit());
				param.setActiveStatus("A");
				param.setRowVersion(actionForm.getRowVersion());
				param.setUpdateBy(actionForm.getUserLoginId());
				
				int rowVersion = common.getRowVersion(Constant.SETUP_PARAM, param.getId());
        		
        		if(rowVersion <= param.getRowVersion()){
        			param.setRowVersion(rowVersion + 1);
        			service.updateSetupParameter(param);  
        			actionForm.setRowVersion(rowVersion + 1);
        			actionForm.setMessage("m_save_success");
        		}
        		
								
			} else if("save".equals(actionForm.getAction())){
				actionForm.setMessage("m_save_fail");
				MSetupParam param = new MSetupParam();
				
				log.debug("Id 					= " + actionForm.getId());
				log.debug("Initial Margin 		= " + actionForm.getInitialMargin());
				log.debug("Maintenance Margin 	= " + actionForm.getMaintenanceMargin());
				log.debug("Fx Margin Limit 	= " + actionForm.getFxMarginLimit());
				log.debug("RowVersion 			= " + actionForm.getRowVersion());	
				
				param.setId(actionForm .getId());
				param.setInitialMargin(actionForm.getInitialMargin());
				param.setMaintenanceMargin(actionForm.getMaintenanceMargin());
				param.setFxMarginLimit(actionForm.getFxMarginLimit());
				param.setActiveStatus("I");
				param.setRowVersion(actionForm.getRowVersion());
				param.setCreateby(actionForm.getUserLoginId());
				param.setUpdateBy(actionForm.getUserLoginId());
				
				int rowVersion = common.getRowVersion(Constant.SETUP_PARAM, param.getId());
        		
        		if(rowVersion <= param.getRowVersion()){
        			param.setRowVersion(rowVersion + 1);
        			service.updateProcess(param);     
        			actionForm.setMessage("m_save_success");
        			actionForm.setRowVersion(rowVersion + 1);
        		} else {
        			log.error("Concurrent Data Access." + Constant.SETUP_PARAM + " [id = " + param.getId() + "]");
        		}
								
			}
			
			log.debug("SetupParameterAction : End");
			
		}catch(Exception e){
			actionForm.setMessage("m_save_fail");
			log.error(e.getMessage());
			return (mapping.findForward("error"));
		}finally{
			if(actionForm != null)
				actionForm.setAction("init");
		}
		
		
		return mapping.findForward("setupParameter");
	}

}
